var fakeWindow = {};var fakeDocument = {};(function(window, document) {})(fakeWindow, fakeDocument);var appConfig = fakeWindow.appOptions || {};

const LIFE_CYCLE_METHODS = ['onLaunch', 'onShow', 'onHide', 'onError', 'onPageNotFound', 'onUnhandledRejection', 'onThemeChange']
const extraConfig = {}
for (const key in appConfig) {
    if (LIFE_CYCLE_METHODS.indexOf(key) === -1) extraConfig[key] = appConfig[key]
}
wx.setStorageSync('cartList', [])
const cartList = wx.getStorageSync('cartList')
const userToken = wx.getStorageSync('userToken')
App({
  globalData: {
    cartList:!!cartList ? []:cartList,
    userInfo: {},
    userToken: !!userToken ? userToken:"",
    URL: 'https://aiwanhe.cn/doufu',
    // URL: 'http://192.168.1.7:7070/doufu',
     //URL: 'http://14.103.193.19:7070/doufu',
    //URL: 'http://127.0.0.1:7070/doufu'
  },
  /**
   * 封装wx.request请求
   * method： 请求方式
   * url: 请求地址
   * data： 要传递的参数
   * callback： 请求成功回调函数
   * errFun： 请求失败回调函数
   **/
  wxRequest(method, url, data, callback, errFun) {
    //this.checkIsLogin();
    let globaUrl= this.globalData.URL
    wx.request({
      url: globaUrl+url,
      method: method,
      data: data,
      header: {
        'content-type': method != 'GET' ? 'application/json' : 'application/x-www-form-urlencoded',
        'Accept': 'application/json',
        'token':this.globalData.userToken
      },
      dataType: 'json',
      success: function (res) {
        if(res.data.code==401){
          wx.setStorageSync('userToken', '');
          //this.globalData.userToken==="";
          //this.showMsg("token 异常请重新登录");
        }
        callback(res.data);
      },
      fail: function (err) {
        console.log("aaaa"+err);
        errFun(err);
      }
    })
  },
   showMsg(msg){
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },
  checkIsLogin(){
    const userToken = this.globalData.userToken
    if (!userToken) {
      this.showMsg("请先登录") 
      setTimeout(()=>{
        // 未登录，跳转到登录页面
        wx.switchTab({
          url: '/pages/me/me'
        });
      },1000)
      return
    }
  },
  setCartList(cartList){
    wx.setStorageSync('cartList',cartList)
    this.globalData.cartList=cartList
  },
  clearCart(){
    wx.setStorageSync('cartList',[])
    this.globalData.cartList=[]
  },
    onLaunch(options) {
      
      console.log("110")
        if (appConfig.onLaunch) appConfig.onLaunch.call(this, options)
    },
    onShow(options) {
        if (appConfig.onShow) appConfig.onShow.call(this, options)
    },
    onHide() {
        if (appConfig.onHide) appConfig.onHide.call(this)
    },
    onError(err) {
        // 支持 window 的 error 事件
        const pages = getCurrentPages() || []
        const currentPage = pages[pages.length - 1]
        if (currentPage && currentPage.window) {
            currentPage.window.$$trigger('error', {
                event: err,
            })
        }

        if (appConfig.onError) appConfig.onError.call(this, err)
    },
    onPageNotFound(options) {
        if (appConfig.onPageNotFound) appConfig.onPageNotFound.call(this, options)
    },
    onUnhandledRejection(options) {
        const pages = getCurrentPages() || []
        const currentPage = pages[pages.length - 1]
        if (currentPage && currentPage.window) {
            const event = new currentPage.window.Event({
                timeStamp: currentPage.window.performance.now(),
                touches: [],
                changedTouches: [],
                name: 'unhandledrejection',
                target: currentPage.window,
                eventPhase: currentPage.window.Event.AT_TARGET,
                $$extra: {
                    promise: options.promise,
                    reason: options.reason,
                }
            })
            currentPage.window.$$trigger('unhandledrejection', {event})
        }

        if (appConfig.onUnhandledRejection) appConfig.onUnhandledRejection.call(this, options)
    },
    onThemeChange(options) {
        if (appConfig.onThemeChange) appConfig.onThemeChange.call(this, options)
    },

    ...extraConfig,
})
